/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bmf.rest;

import com.bmf.dao.HLmanager;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import network.manager.nodesAndGroupsManager.TooManyGroupsCreatedException;

/**
 *
 * @author Tony_
 */
@Startup
@Singleton
public class nManager implements nManagerLocal,Observer {

    private HLmanager manager;
    private int id;
    private Group g;
    private Request r;
    
    //la nostra proprietà da iniettare
    public ArrayList<Node> nodes=new ArrayList<Node>();
    public ArrayList<Group> groups=new ArrayList<Group>();
    public ArrayList<Request> requests=new ArrayList<Request>();
    
    public nManager(){
    }
    
    //SI AGGIUNGE AGLI OSSERVATORI E FA PARTIRE HLMANAGER 
    @PostConstruct
    @Override
    public void startBmf() {  //throws InvalidPacketParametersException, IOException, TooManyGroupsCreatedException {
        System.out.println("ICEBMF: STARTING HIGH LEVEL MANAGER");
        g=new Group("1","default");
        r=new Request("1",3,30,6);
        requests.add(r);
        groups.add(g);
        try {
            manager=new HLmanager();
        } catch (TooManyGroupsCreatedException ex) {
            Logger.getLogger(nManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        manager.addObserver(this);
        System.out.println("ICEBMF: BEAN IS LISTENING TO THE MANAGER");
       
    }

    @Override
    public void update(Observable o, Object arg) {
        if(arg instanceof Integer){
            this.id=(Integer)arg;
            System.out.println("NODO "+id+" RICEVUTO!");
            Node test=new Node();
            test.setGroup(g);
            test.setId(Integer.toString(id));
            test.setSensorBoardName("Light");
            nodes.add(test);
            System.out.println("NODO "+id+" REGISTRATO!");
        }    
    }
    @Override
    public void put(Group g){
        manager.addGroup(g);
    }
    
    @Override
    public void putR(Request r){
       this.requests.add(r);
    }
    
    @Override
    public ArrayList<Node> getNodes(){
         return this.nodes;
    }
    public ArrayList<Node> getNodes(String id){
        ArrayList<Node> c=new ArrayList<Node>();
        for(int i=0;i<nodes.size();i++){
            if(nodes.get(i).getGroup().getId().equals(id)){
                c.add(nodes.get(i));
            }
        } 
        return c;
    }
    public ArrayList<Node> getNodesN(String name){
        ArrayList<Node> c=new ArrayList<Node>();
        for(int i=0;i<nodes.size();i++){
            if(nodes.get(i).getGroup().getId().equals(name)){
                c.add(nodes.get(i));
            }
        } 
        return c;
    }
    @Override
    public void setNodes(ArrayList<Node> nodes){
        this.nodes=nodes;
    }

    @Override
    public HLmanager getManager() {
        return manager;
    }

    public void setManager(HLmanager manager) {
        this.manager = manager;
    }

    @Override
    public ArrayList<Group> getGroups() {
        return groups;
    }

    @Override
    public void setGroups(ArrayList<Group> groups) {
        this.groups = groups;
    }

    @Override
    public ArrayList<Request> getRequests() {
        return requests;
    }

    public void setRequests(ArrayList<Request> requests) {
        this.requests = requests;
    }

}
